package com.core.dto;

import com.core.model.xg.Refrigeration;
import com.core.shared.DigitalHandle;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author lxf
 * @date 2018/5/2 17:27
 * To change this template use Editor | File and Code Templates.
 */
public class RefrigerationDTO {

    private String id;
    private String date;
    private String project;
    private String income;
    private String expenditure;
    private String balance;
    private String excutorId;
    private ExcutorDTO excutor;
    private Map<String, String> executorMaps;

    public RefrigerationDTO() {
    }

    public RefrigerationDTO(String id, String date, String project, String income, String expenditure, String balance, ExcutorDTO excutor) {
        this.id = id;
        this.date = date;
        this.project = project;
        this.income = income;
        this.expenditure = expenditure;
        this.balance = balance;
        this.excutor = excutor;
    }

    public static RefrigerationDTO toDTO(Refrigeration refrigeration) {
        //先把收入与支出转为保留小数点后两位，再进行减法运算，最后除以1是确保结果只保留小数点后两位
        BigDecimal balance = DigitalHandle.subtract(new BigDecimal(new DecimalFormat("#0.00").format(refrigeration.income())), new BigDecimal(new DecimalFormat("#0.00").format(refrigeration.expenditure())));
        BigDecimal one = new BigDecimal("1");
        balance = balance.divide(one, 2, BigDecimal.ROUND_DOWN);
        return new RefrigerationDTO(refrigeration.id(), refrigeration.date(), refrigeration.project(), new DecimalFormat("#0.00").format(refrigeration.income()), new DecimalFormat("#0.00").format(refrigeration.expenditure()), balance.toString(), ExcutorDTO.toDTO(refrigeration.excutor()));
    }

    public static List<RefrigerationDTO> toDTOs(List<Refrigeration> refrigerations) {
        List<RefrigerationDTO> list = new ArrayList<>();
        for (Refrigeration refrigeration : refrigerations) {
            list.add(toDTO(refrigeration));
        }
        return list;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public String getIncome() {
        return income;
    }

    public double getIncomeForDouble() {
        return StringUtils.isEmpty(income) ? 0 : Double.parseDouble(income);
    }

    public void setIncome(String income) {
        this.income = income;
    }

    public String getExpenditure() {
        return expenditure;
    }

    public double getExpenditureForDouble(){
        return StringUtils.isEmpty(expenditure) ? 0 : Double.parseDouble(expenditure);
    }

    public void setExpenditure(String expenditure) {
        this.expenditure = expenditure;
    }

    public ExcutorDTO getExcutor() {
        return excutor;
    }

    public void setExcutor(ExcutorDTO executor) {
        this.excutor = executor;
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public Map<String, String> getExecutorMaps() {
        return executorMaps;
    }

    public void setExecutorMaps(Map<String, String> executorMaps) {
        this.executorMaps = executorMaps;
    }

    public String getExcutorId() {
        return excutorId;
    }

    public void setExcutorId(String excutorId) {
        this.excutorId = excutorId;
    }

}
